package com.startech.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.startech.common.exception.ServiceException;
import com.startech.domain.User;
import com.startech.domain.UserInfo;
import com.startech.dto.LoginDto;
import com.startech.dto.RegisterDto;
import com.startech.mapper.UserMapper;
import com.startech.service.IUserInfoService;
import com.startech.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author startech
 * @since 2025-07-17
 */
@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    private final IUserInfoService userInfoService;
    @Override
    public void register(RegisterDto registerDto) {
        User user = new User();
        BeanUtils.copyProperties(registerDto, user);
        this.save(user);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registerDto, userInfo);
        userInfo.setUserId(user.getId());
        userInfoService.save(userInfo);
    }

    @Override
    public void login(LoginDto loginDto) {
        String email = loginDto.getEmail();
        if (StringUtils.isEmpty(email)){
            throw new ServiceException("邮箱不能为空");
        }
        LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery()
                .eq(User::getEmail, email)
                .eq(User::getPassword, loginDto.getPassword());
        User user = this.getOne(queryWrapper);
        if (user == null){
            throw new ServiceException("用户名或密码错误");
        }

    }
}
